package org.jivesoftware.smack.debugger;

import java.io.PrintStream;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.ObservableReader;
import org.jivesoftware.smack.util.ObservableWriter;
import org.jivesoftware.smack.util.ReaderListener;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.WriterListener;

public class ConsoleDebugger
  implements SmackDebugger
{
  public static boolean printInterpreted = false;
  private ConnectionListener connListener;
  private Connection connection;
  private SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
  private PacketListener listener;
  private Reader reader;
  private ReaderListener readerListener;
  private Writer writer;
  private WriterListener writerListener;

  public ConsoleDebugger(Connection paramConnection, Writer paramWriter, Reader paramReader)
  {
    this.connection = null;
    this.listener = null;
    this.connListener = null;
    this.connection = paramConnection;
    this.writer = paramWriter;
    this.reader = paramReader;
    createDebug();
  }

  private void createDebug()
  {
    ObservableReader localObservableReader = new ObservableReader(this.reader);
    this.readerListener = new ReaderListener(this)
    {
      public void read()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " RCV  (" + ConsoleDebugger.access$1(this.this$0).hashCode() + "): " + paramString);
      }
    };
    localObservableReader.addReaderListener(this.readerListener);
    ObservableWriter localObservableWriter = new ObservableWriter(this.writer);
    this.writerListener = new WriterListener(this)
    {
      public void write()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " SENT (" + ConsoleDebugger.access$1(this.this$0).hashCode() + "): " + paramString);
      }
    };
    localObservableWriter.addWriterListener(this.writerListener);
    this.reader = localObservableReader;
    this.writer = localObservableWriter;
    this.listener = new PacketListener(this)
    {
      public void processPacket()
      {
        if (ConsoleDebugger.printInterpreted)
          System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " RCV PKT (" + ConsoleDebugger.access$1(this.this$0).hashCode() + "): " + paramPacket.toXML());
      }
    };
    this.connListener = new ConnectionListener(this)
    {
      public void connectionClosed()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " Connection closed (" + ConsoleDebugger.access$1(this.this$0).hashCode() + ")");
      }

      public void connectionClosedOnError()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " Connection closed due to an exception (" + ConsoleDebugger.access$1(this.this$0).hashCode() + ")");
        paramException.printStackTrace();
      }

      public void reconnectingIn()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " Connection (" + ConsoleDebugger.access$1(this.this$0).hashCode() + ") will reconnect in " + paramInt);
      }

      public void reconnectionFailed()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " Reconnection failed due to an exception (" + ConsoleDebugger.access$1(this.this$0).hashCode() + ")");
        paramException.printStackTrace();
      }

      public void reconnectionSuccessful()
      {
        System.out.println(ConsoleDebugger.access$0(this.this$0).format(new Date()) + " Connection reconnected (" + ConsoleDebugger.access$1(this.this$0).hashCode() + ")");
      }
    };
  }

  public Reader getReader()
  {
    return this.reader;
  }

  public PacketListener getReaderListener()
  {
    return this.listener;
  }

  public Writer getWriter()
  {
    return this.writer;
  }

  public PacketListener getWriterListener()
  {
    return null;
  }

  public Reader newConnectionReader(Reader paramReader)
  {
    ((ObservableReader)this.reader).removeReaderListener(this.readerListener);
    ObservableReader localObservableReader = new ObservableReader(paramReader);
    localObservableReader.addReaderListener(this.readerListener);
    this.reader = localObservableReader;
    return this.reader;
  }

  public Writer newConnectionWriter(Writer paramWriter)
  {
    ((ObservableWriter)this.writer).removeWriterListener(this.writerListener);
    ObservableWriter localObservableWriter = new ObservableWriter(paramWriter);
    localObservableWriter.addWriterListener(this.writerListener);
    this.writer = localObservableWriter;
    return this.writer;
  }

  public void userHasLogged(String paramString)
  {
    String str1;
    boolean bool = "".equals(StringUtils.parseName(paramString));
    StringBuilder localStringBuilder = new StringBuilder("User logged (").append(this.connection.hashCode()).append("): ");
    if (bool)
      str1 = "";
    while (true)
    {
      String str2 = localStringBuilder.append(str1).append("@").append(this.connection.getServiceName()).append(":").append(this.connection.getPort()).toString() + "/" + StringUtils.parseResource(paramString);
      System.out.println(str2);
      this.connection.addConnectionListener(this.connListener);
      return;
      str1 = StringUtils.parseBareAddress(paramString);
    }
  }
}